# Contributor: Mehdi Chinoune <mehdi.chinoune@hotmail.com>

if [[ $MINGW_PACKAGE_PREFIX == *-clang-* ]]; then
  _clangprefix=1
fi

_realname=openmp
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
_version=15.0.7
_rc=""
_tag=llvmorg-${_version}${_rc}
pkgver=${_version}${_rc/-/}
pkgrel=1
pkgdesc="LLVM OpenMP Library (mingw-w64)"
url="https://openmp.llvm.org/"
arch=(any)
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32' 'clangarm64')
license=("custom:Apache 2.0 with LLVM Exception")
groups=($( (( _clangprefix )) && echo "${MINGW_PACKAGE_PREFIX}-toolchain"))
provides=($( (( _clangprefix )) && echo "${MINGW_PACKAGE_PREFIX}-omp"))
depends=($( (( _clangprefix )) || echo "${MINGW_PACKAGE_PREFIX}-gcc-libs"))
makedepends=("${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-cc"
             $( (( _clangprefix )) || echo "${MINGW_PACKAGE_PREFIX}-gcc-fortran")
             $( [[ $CARCH != *86* ]] || echo "${MINGW_PACKAGE_PREFIX}-uasm" ))
options=('!debug' 'strip')
_url=https://github.com/llvm/llvm-project/releases/download/${_tag}
_pkgfn=$_realname-$pkgver.src
source=($_url/$_pkgfn.tar.xz{,.sig}
        ${_url}/cmake-${pkgver}.src.tar.xz{,.sig}
        "001-cast-to-make-gcc-happy.patch"
        "002-hacks-for-static-linking.patch"
        "https://github.com/llvm/llvm-project/commit/cea951dccd9aaa7fee8a60ecf9170d7d3cc35086.patch"
        "D137168.patch")
sha256sums=('3f168d38e7a37b928dcb94b33ce947f75d81eef6fa6a4f9d16b6dc5511c07358'
            'SKIP'
            '8986f29b634fdaa9862eedda78513969fe9788301c9f2d938f4c10a3e7a3e7ea'
            'SKIP'
            '11352ffbe7559a7170f2abd52b3552c877fbcf8fc82cff77b421e8b130a4dd66'
            '7238f009264bc1b162b73ca3a2a0b224b65ec3ed384304f3e5464032c4c026f4'
            '159f038084d3999d0329db33f283b3440c47db7b2f1587fd0d158450647669a5'
            '9e50acee4bf4aff2b3f5091c33ab7b7a1e9608cb863e2434554d2333522f4a40')
validpgpkeys=('B6C8F98282B944E3B0D5C2530FC3042E345AD05D'  # Hans Wennborg, Google.
              '474E22316ABF4785A88C6E8EA2C794A986419D8A'  # Tom Stellard
              'D574BD5D1D0E98895E3BF90044F2485E45D59042') # Tobias Hieta

apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying ${_patch}"
    patch -Nbp1 -i "${srcdir}/${_patch}"
  done
}

prepare() {
  cd "${srcdir}"
  mv cmake-$pkgver.src cmake

  cd ${srcdir}/$_pkgfn
  if (( ! _clangprefix )); then
    apply_patch_with_msg \
      "001-cast-to-make-gcc-happy.patch"
  fi
  apply_patch_with_msg 002-hacks-for-static-linking.patch

  # https://github.com/llvm/llvm-project/issues/56349
  patch -Nbp2 -i "${srcdir}/cea951dccd9aaa7fee8a60ecf9170d7d3cc35086.patch"
  # https://reviews.llvm.org/D137168
  patch -Nbp2 -i "${srcdir}/D137168.patch"
}

build() {
  cd ${srcdir}
  rm -rf build-${MSYSTEM}-{shared,static}

  if check_option "debug" "y"; then
    _build_type="Debug"
  else
    _build_type="Release"
  fi

  local -a platform_config
  case "${CARCH}" in
    x86_64)
      export ASM_COMPILER_FLAGS=-win64
      platform_config+=(-DLIBOMP_ASMFLAGS="${ASM_COMPILER_FLAGS}")
      ;& # fall-through
    i?86)
      export ASM_COMPILER="${MINGW_PREFIX}/bin/uasm.exe"
      platform_config+=(-DCMAKE_ASM_MASM_COMPILER="${ASM_COMPILER}")
      ;;
  esac

  for shared_libs in shared static; do
    MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    ${MINGW_PREFIX}/bin/cmake \
      -GNinja \
      -DCMAKE_BUILD_TYPE=${_build_type} \
      -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
      -DCMAKE_SYSTEM_IGNORE_PATH=/usr/lib \
      -DLIBOMP_FORTRAN_MODULES="$( (( _clangprefix )) && echo OFF || echo ON )" \
      -DLIBOMP_ENABLE_SHARED="$( [[ $shared_libs == static ]] && echo OFF || echo ON )" \
      "${platform_config[@]}" \
      -Wno-dev \
      -S $_pkgfn \
      -B "build-${MSYSTEM}-${shared_libs}"
    ${MINGW_PREFIX}/bin/cmake --build "build-${MSYSTEM}-${shared_libs}"
  done
  unset shared_libs
}

package() {
  DESTDIR=${pkgdir} ${MINGW_PREFIX}/bin/cmake --install "${srcdir}/build-${MSYSTEM}-shared"
  DESTDIR=${pkgdir} ${MINGW_PREFIX}/bin/cmake --install "${srcdir}/build-${MSYSTEM}-static"

  install -d "$pkgdir${MINGW_PREFIX}"/share/licenses/${_realname}
  install -Dm644 $_pkgfn/LICENSE* -t "$pkgdir${MINGW_PREFIX}"/share/licenses/${_realname}
}
